#include "stdafx.h"
#include "StatsPage.h"
#include "ui_MainWindow.h"
#include "Diagram.h"

using namespace AirCrash::Calypso;

StatsPage::StatsPage() : mUi(0), mThread(new StatsThread()) {
	connect( mThread, SIGNAL(result(QImage)), this, SLOT(result(QImage)) );
}

StatsPage::~StatsPage() {
	mThread->wait();
	delete mThread;
}

void StatsPage::showEvent(QShowEvent * e) {
	if (mUi && !e->spontaneous()) {
		mUi->mStatsTime->setText(Config::get("Stats", "timeDiff").toString());
		mUi->mMenuBar->setVisible(true);
		mUi->mStatsResult->clear();
		mThread->start();
	}
}

void StatsPage::result(QImage resultImage) {
	QIcon icon(QPixmap::fromImage(resultImage));
	QListWidgetItem * item = new QListWidgetItem(icon, "");
	mUi->mStatsResult->addItem(item);
}

void StatsPage::setUi(Ui::MainWindowClass * ui) {
	mUi = ui;
	connect( mUi->mSetStatsTime, SIGNAL(clicked()), this, SLOT(setTime()) );
	mUi->mStatsResult->setIconSize(mUi->mStatsResult->size());
	mUi->mStatsResult->setGridSize(mUi->mStatsResult->size());
}

void StatsPage::setTime() {
	Config::set("Stats", "timeDiff", mUi->mStatsTime->text().toInt());
	Config::sync();
	mUi->mStatsResult->clear();
	mThread->start();
}

StatsThread::StatsThread() {}
StatsThread::~StatsThread() {}

void StatsThread::run() {

	QSqlQuery query;


	//Scanned Items
	QMap<QDateTime, int> scanned;

	query.exec("SELECT Date FROM Scanned");

	const int timeDiff(Config::get("Stats", "timeDiff").toInt());
	while (query.next()) {
		QDateTime scannedTime(QDateTime::fromString(query.value(0).toString(), "dd-MM-yyyy hh:mm:ss"));

		int minutes((scannedTime.time().hour() * 60) + scannedTime.time().minute());
		minutes /= timeDiff;
		minutes *= timeDiff;
		
		int hours(minutes / 60);
		minutes -= (hours * 60);

		QDateTime newTime(scannedTime.date(), QTime(hours, minutes, 0));
		
		if (scanned.find(newTime) != scanned.end()) {
			++scanned[newTime];
		} else {
			scanned.insert(newTime, 1);
		}
	}

	QMapIterator<QDateTime, int> it(scanned);
	QDate lastDate;

	QMap<QString, int> values;
	bool createImage(false);
	while (it.hasNext()) {
		it.next();
		if (lastDate != it.key().date()) {
			if (!values.isEmpty()) {
				emit result( Diagram::createDiagram(values, lastDate.toString("dddd dd MMMM yyyy")) );
				values.clear();
			}
			lastDate = it.key().date();
		}
		if (timeDiff == 1) {
			values.insert(it.key().toString("hh:mm"), it.value());
		} else {
			values.insert(it.key().toString("hh:mm") + "\n" + it.key().addSecs(timeDiff * 60).toString("hh:mm"), it.value());
		}
	}
	if (!values.isEmpty()) {
		emit result( Diagram::createDiagram(values, lastDate.toString("dddd dd MMMM yyyy")) );
		values.clear();
	}

	//Hoofdprijzen
	if (!query.exec("SELECT Codes.Id, date FROM Scanned INNER JOIN Codes ON Scanned.Code = Codes.Id INNER JOIN Items ON Codes.ItemId = Items.Id WHERE Items.Jackpot = 'true'")) {
		qDebug() << "Error: " << query.lastError().text();
	}
	QStringList jackpotItems;
	while (query.next()) {
		jackpotItems.push_back(query.value(0).toString() + "  ====>  " + query.value(1).toString());
	}

	QImage jackpotImage(400, 50 + ((jackpotItems.size()) * 30), QImage::Format_ARGB32);
	
	QPainter painter(&jackpotImage);

	painter.fillRect(jackpotImage.rect(), Qt::white);
	painter.drawRect(QRect(1, 1, jackpotImage.width() - 2, jackpotImage.height() - 2));

	painter.setPen(Qt::black);

	painter.drawText(QRect(0, 0, jackpotImage.width(), 50), Qt::AlignCenter, "Hoofdprijzen");
	int numItem(0);
	foreach (QString jackpotItem, jackpotItems) {
		painter.drawText(QRect(20, 50 + (numItem * 30), 380, 30), Qt::AlignLeft, jackpotItem);

		++numItem;
	}

	emit result(jackpotImage);
}